<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <template id="t1">
      <div id="box">
        <form>
          <p>
            用户名
            <input type="text" @input="checkUsername" />
            <span v-if="usernameError">用户名必须3-18个字符</span>
          </p>
        </form>
      </div>
    </template>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        template: "#t1",
        data: {
          usernameError: false,
        },
        methods: {
          checkUsername(e) {
            const value = e.target.value;
            if (/^\w{2,18}$/.test(value)) {
              this.usernameError = false;
            } else {
              this.usernameError = true;
            }
          },
        },
      });
    </script>
  </body>
</html>
